<template>
  <view class="cityPosition_container app_container_page">
    <city-select @cityClick="cityClick" :activeCity="activeCity" :historyCity="historyCity" :hotCity="hotCity" :isSearch="true" ref="citys"></city-select>
  </view>
</template>

<script>
import citySelect from '@/components/app_component/city-select/city-select.vue'
export default {
  components: {
    citySelect
  },
  data() {
    return {
      // 当前城市
      activeCity: {
        cityCode: '1',
        cityName: '定位中...'
      },
      // 历史
      historyCity: [],
      historyCity1: [],
      // 热门城市
      hotCity: [
        {
          cityCode: '110100',
          cityName: '北京'
        },
        {
          cityCode: '310100',
          cityName: '上海'
        },
        {
          cityCode: '440100',
          cityName: '广州'
        },
        {
          cityCode: '440300',
          cityName: '深圳'
        },
        {
          cityCode: '330100',
          cityName: '杭州'
        },
        {
          cityCode: '320100',
          cityName: '南京'
        },
        {
          cityCode: '510100',
          cityName: '成都'
        },
        {
          cityCode: '120100',
          cityName: '天津'
        },
        {
          cityCode: '370200',
          cityName: '青岛'
        }
      ]
    }
  },
  methods: {
    cityClick(item) {
      if (item.cityCode !== '1') {
        if (item.cityCode === '0') {
          this.positionFn()
        } else {
          this.activeCity = item
          this.$store.commit('SET_POSITION', item)
          uni.navigateBack()
          const nowCity = JSON.parse(uni.getStorageSync('nowCity'))
          if (uni.getStorageSync('historyCity')) {
            this.historyCity1 = JSON.parse(uni.getStorageSync('historyCity'))
          } else {
            this.historyCity1 = []
          }
          if (nowCity.cityName !== item.cityName) {
            let flag = true
            for (const i in this.historyCity1) {
              if (this.historyCity1[i].cityName === item.cityName) {
                flag = false
                break
              }
            }
            if (flag) {
              this.historyCity1.push(item)
            }
            this.historyCity = this.historyCity1
            uni.setStorageSync('historyCity', JSON.stringify(this.historyCity1))
          }
        }
      }
    },
    async positionFn() {
      try {
        const res = await this.$uniPromisify.getLocation()
        const latitude = res.latitude
        const longitude = res.longitude
        const result = await this.$uniPromisify.request({
          method: 'GET',
          url: 'https://restapi.amap.com/v3/geocode/regeo?key=eb41d25b841f6e35549544e571bae6d2&' + '&location=' + longitude + ',' + latitude
        })
        this.activeCity = {
          cityName: result.regeocode.addressComponent.city.substring(0, result.regeocode.addressComponent.city.length - 1),
          cityCode: result.regeocode.addressComponent.adcode
        }
        uni.setStorageSync('nowCity', JSON.stringify(this.activeCity))
      } catch (error) {
        console.log(error)
        this.activeCity = {
          cityName: '定位失败',
          cityCode: '0'
        }
      }
    }
  },
  onLoad() {
    this.positionFn()
  },
  onShow() {
    if (uni.getStorageSync('historyCity')) {
      this.historyCity = JSON.parse(uni.getStorageSync('historyCity'))
    }
  }
}
</script>
